<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

if (($_REQUEST["agent_type_id"]) AND ( $_REQUEST["name_short"])) 
{
	$agent_type_id = strip_tags($_REQUEST["agent_type_id"]);
	$name_short = strip_tags($_REQUEST["name_short"]);		

	file_put_contents  ( "selectBy_AgentTypeNameShort.txt", 
							"agent_type_id '" .$agent_type_id. 
							"' name_short '" .$name_short."'");
	
	//$agent_type_id = '12';
	//$name_short = 'Ma';
	
	$query = new Doctrine_Query();
    $query->from('Agent a')
    			->where('a.agent_type_id ='.$agent_type_id.' AND a.deleted=0
    				AND a.name_full LIKE ?', $name_short.'%'  )
            	->orderby('name_full');
                  
    $items = $query->execute();  

}
elseif ($_REQUEST["agent_type_id"]) 
{
	$agent_type_id = strip_tags($_REQUEST["agent_type_id"]);
	file_put_contents  ( "selectBy_AgentType.txt", $agent_type_id);
	
	$query = new Doctrine_Query();
    $query->from('Agent a')
    			->where('a.agent_type_id ='.$agent_type_id.'
    					AND a.deleted = 0')
    			->orderby('name_full');
                  
    $items = $query->execute();  
}

    
$dom = new DomDocument('1.0',"UTF-8");
$dom->formatOutput = true;
$root = $dom->createElement( "results" );
$dom->appendChild( $root );

	foreach ($items as $item){
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'agent_id', $item['agent_id'] );
		$dn->setAttribute( 'name_short', utf8_encode($item['name_short']));
		$dn->setAttribute( 'name_full', utf8_encode($item['name_full']));
		//$dn->setAttribute( 'name_short', $item['name_short']);
		//$dn->setAttribute( 'name_full', $item['name_full']);
		$root->appendChild( $dn );
}		

header( "Content-type: text/xml");
echo $dom->saveXML();


?>
	
